﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WebSite.FengYi.Forms
{
    public partial class FormSettings : Form
    {
        public FormSettings()
        {
            InitializeComponent();
        }
        private String openExe()
        {

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件夹";
            dialog.Filter = "Bat文件|*.bat|Exe文件|*.exe|所有文件|*.*";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = dialog.FileName;
                return file;
            }
            else
            {
                return "";
            }

        }
        private void btNginx_Click(object sender, EventArgs e)
        {
            tbNginx.Text = openExe();
        }

        private void btnMysql_Click(object sender, EventArgs e)
        {
            tbMysql.Text = openExe();
        }

        private void btnRedis_Click(object sender, EventArgs e)
        {
            tbRedis.Text = openExe();
        }

        private void btnTomcat_Click(object sender, EventArgs e)
        {
            ///tbTomcat.Text = openExe();
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                String savePath = dialog.SelectedPath;
                tbTomcat.Text = savePath;
            }
        }

        private void btnSpringBoot_Click(object sender, EventArgs e)
        {

            //tbSpringBoot.Text = openExe();
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                String savePath = dialog.SelectedPath;
                tbSpringBoot.Text = savePath;
            }
        }

        private void btnES_Click(object sender, EventArgs e)
        {
            tbES.Text = openExe();
        }

        private void btnSvrSave_Click(object sender, EventArgs e)
        {
            UpdateAppConfig("cbDir", cbDir.Text);
            UpdateAppConfig("Nginx", tbNginx.Text);
            UpdateAppConfig("Tomcat", tbTomcat.Text);
            UpdateAppConfig("Mysql", tbMysql.Text);
            UpdateAppConfig("Redis", tbRedis.Text);
            UpdateAppConfig("ES", tbES.Text);
            UpdateAppConfig("SpringBoot", tbSpringBoot.Text);
            MessageBox.Show("保存成功");
        }
        public static void UpdateAppConfig(string newKey, string newValue)
        {
            string file = System.Windows.Forms.Application.ExecutablePath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            bool exist = false;
            foreach (string key in config.AppSettings.Settings.AllKeys)
            {
                if (key == newKey)
                {
                    exist = true;
                }
            }
            if (exist)
            {
                config.AppSettings.Settings.Remove(newKey);
            }
            config.AppSettings.Settings.Add(newKey, newValue);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        private void FormSettings_Load(object sender, EventArgs e)
        {
            cbDir.Text = ConfigurationManager.AppSettings["cbDir"];
            tbNginx.Text = ConfigurationManager.AppSettings["Nginx"];
            tbTomcat.Text = ConfigurationManager.AppSettings["Tomcat"];
            tbMysql.Text = ConfigurationManager.AppSettings["Mysql"];
            tbRedis.Text = ConfigurationManager.AppSettings["Redis"];
            tbES.Text = ConfigurationManager.AppSettings["ES"];
            tbSpringBoot.Text = ConfigurationManager.AppSettings["SpringBoot"];

        }
    }
}
